package com.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.TypeReference;
import com.config.RestTemplateConfig;
import com.service.RegistryService;
import com.service.Vo.RegistryBean;
import com.service.Vo.RegistryTagBean;
import com.util.TokenRequest;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Import;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@EnableAutoConfiguration
@Import(value={RestTemplateConfig.class})
@Service
public class RegistryServiceImpl implements RegistryService {

    RegistryBean registryBean;
    RegistryTagBean registryTagBean;

    @Autowired
    TokenRequest tokenRequest;

    @Override
    public List<RegistryBean> RegistryList(String regname) throws Exception {
        RestTemplate client = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        String token = tokenRequest.getToken();
        headers.add("X-Auth-Token", token);
        HttpEntity<String> entity = new HttpEntity<String>(headers);
        HttpEntity<String> response = client.exchange(
                "https://swr-api.cn-east-2.myhuaweicloud.com/v2/manage/repos?filter=namespace::" + regname, HttpMethod.GET, entity, String.class);
        String json = response.getBody();

        ArrayList<RegistryBean> registrylist = JSON.parseObject(json, new TypeReference<ArrayList<RegistryBean>>() {
        });
        int count = 0;
        for (RegistryBean listmap : registrylist) {
            count = count + 1;
            listmap.setId(count);
            listmap.setText(listmap.getName());
        }
        return registrylist;
    }


    @Override
    public List<RegistryTagBean> RegistryTagList(String namespace, String reponame) throws Exception {
        RestTemplate client = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        String token = tokenRequest.getToken();
        String url =  "https://swr-api.cn-east-2.myhuaweicloud.com/v2/manage/namespaces/" + namespace + "/repos/" + reponame + "/tags";
        headers.add("X-Auth-Token", token);
        HttpEntity<String> entity = new HttpEntity<String>(headers);
         HttpEntity<String> response = client.exchange(
                url, HttpMethod.GET, entity, String.class);
        String json = response.getBody();

        ArrayList<RegistryTagBean> registrytaglist = JSON.parseObject(json, new TypeReference<ArrayList<RegistryTagBean>>() {
        });
        int count = 0;
        for (RegistryTagBean listmap : registrytaglist) {
            count = count + 1;
            listmap.setId(count);
        }
        return registrytaglist;
    }
}
